﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Data.Common;
using StampController.BL.Models;
using StampController.Core;

namespace StampController.BL
{
    /// <summary>
    /// Add and list feedbacka from user
    /// </summary>
    public class FeedbackFormImpl
    {
        /// <summary>
        /// Get all feedback from users
        /// </summary>
        /// <returns></returns>
        public IList<FeedbackModel> GetFeedback()
        {
            var list = new List<FeedbackModel>();
            using (var dbhelper = new DbHelper())
            {
                using (DbDataReader reader = dbhelper.ExecuteReader("select * from Feedback"))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            list.Add(new FeedbackModel
                            {
                                FeedbackID = reader.Get<int>(0),
                                Subject = reader.GetString(1),
                                Comment = reader.GetString(2),
                                UserID = reader.GetString(3),
                                FeedbackTime = reader.GetDateTime(4)
                            });
                        }
                    }
                    reader.Close();
                }
                return list;
            }
        }

        /// <summary>
        /// Get feedback list by page number
        /// </summary>
        /// <param name="user"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<FeedbackModel> GetFeedbackList(int? pageNumber, int? pageSize, out int count)
        {
            try
            {
                using (var dbHelper = new DbHelper())
                {
                    using (DataTable dt = dbHelper.GetDataTable("SELECT Feedback.*, Users.Name as Usersname FROM Users INNER JOIN Feedback ON Users.UserID = Feedback.UserID"))
                    {
                        var list = dt.Select().Select(row => new FeedbackModel
                        {
                            FeedbackID = row.Cells<int>("FeedbackID"),
                            Subject = row.Cells<string>("Subject"),
                            Comment = row.Cells<string>("Comment"),
                            UserID = row.Cells<string>("Usersname"),
                            FeedbackTime = row.Cells<DateTime>("FeedbackTime")
                        }).ToList();

                        count = list.Count();
                        if (pageNumber.HasValue && pageSize.HasValue)
                            return list.Page(pageNumber.Value, pageSize.Value);
                        return list;
                    }
                }
            }
            catch
            {
                count = 0;
                return new List<FeedbackModel>();
            }
        }

        // Save feedaback from user
        public bool SaveFeedback(FeedbackModel fback)
        {
            using (var helper = new DbHelper())
            {
                try
                {
                    helper.TransactionHandler(DbHelper.TransactionType.Open);
                    object[,] parameter;
                    parameter = new object[,]
                    {
                        {"@Subject",fback.Subject},
                        {"@Comment",fback.Comment},
                        {"@UserID",fback.UserID},
                        {"@FeedbackTime",fback.FeedbackTime}
                    };
                    helper.ExecuteNonQuery(@"Insert into Feedback(Subject,Comment,UserID,FeedbackTime)
                    values (@Subject,@Comment,@UserID,@FeedbackTime)", CommandType.Text, parameter);
                    helper.TransactionHandler(DbHelper.TransactionType.Commit);
                }
                catch
                {
                    helper.TransactionHandler(DbHelper.TransactionType.Rollback);
                    return false;
                }
            }
            return true;
        }

        // Get fee
    }
}
